package info.macnana.online.oauth.service.impl;

import info.macnana.online.jwt.helper.JwtHelper;
import info.macnana.online.oauth.model.AccessToken;
import info.macnana.online.oauth.service.AuthService;
import info.macnana.online.user.model.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * author: zhengheng
 * github: https://github.com/macnana777
 * email: 517862340@qq.com
 * <p>
 * Date: 2017-08-03 10:08
 * Description:
 * Copyright(©) 2017 by zhengheng.
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public User register(User user) {
        return null;
    }

    @Override
    public AccessToken login(String username, String password) {
        AccessToken accessToken = null;
        if(StringUtils.equals(username,"macnana") && StringUtils.equals(password,"macnana")){
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setRoles(Arrays.asList("ROLE_ADMIN","ROLE_USER"));
            String token = jwtHelper.createJWT(user);
            accessToken = new AccessToken();
            accessToken.setAccess_token(token);
            accessToken.setToken_type("test");
            accessToken.setExpires_in(jwtHelper.getExpirationDateFromToken(token));
        }
        return accessToken;
    }

    @Override
    public AccessToken refresh(String oldToken) {
        return null;
    }
}
